package info.niwota.home.fm;

import info.niwota.home.HomeApplication;
import info.niwota.home.R;
import x.java.io.File;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

public final class IconUtils {

	private static final int[] DRAWABLES_FOLDER = { R.drawable.ic_top,
			R.drawable.x_folder, R.drawable.x_link};

	private static final int[] DRAWABLES_GROUP = { R.drawable.recent,
			R.drawable.superstar, R.drawable.fave, -1 /* all */, };

	private static final int[] DRAWABLES_TYPE = { R.drawable.x_app,
			R.drawable.x_audio, R.drawable.x_html, R.drawable.x_image,
			R.drawable.x_text, R.drawable.x_video, };


	private static final String[] MIME_TYPE = { "application/", "audio/",
			"text/html", "image/", "text/", "video/", };
	
	private static BitmapDrawable[] imagesFolder = new BitmapDrawable[DRAWABLES_FOLDER.length];

	private static Bitmap[] imagesGroup = new Bitmap[DRAWABLES_GROUP.length];

	private static BitmapDrawable[] imagesType = new BitmapDrawable[DRAWABLES_TYPE.length];
	
	static {
		init();
	}

	public static void init() {
		try {
			Context ctx = HomeApplication.context;
			//wait if ctx is null??
			Resources res = ctx.getResources();
			initImages(res, DRAWABLES_GROUP, imagesGroup);
			initImages(res, DRAWABLES_TYPE, imagesType);
			initImages(res, DRAWABLES_FOLDER, imagesFolder);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void setFileIcon(ImageView v, File f) {
		boolean b = setIcon(v, f);
		if (b) {
			return;
		}
		// default file icon
		String ct = f.getContentType();
		int len = MIME_TYPE.length;
		for (int i = 0; i < len; i++) {
			if (ct.startsWith(MIME_TYPE[i])) {
				v.setImageDrawable(imagesType[i]);
				return;
			}
		}
		v.setImageDrawable(imagesType[0]);
	}

	public static boolean setIcon(ImageView v, File f) {
		Object obj = f.getIcon();
		if (obj == null) {
			return false;
		}
		if (obj instanceof Bitmap) {
			Bitmap bm = (Bitmap) obj;
			v.setImageBitmap(bm);
			return true;
		} else if (obj instanceof Drawable) {
			Drawable dr = (Drawable) obj;
			v.setImageDrawable(dr);
			return true;
		}
		return false;
	}

	public static void setFolderIcon(ImageView v, File f) {
		boolean b = setIcon(v, f);
		if (b) {
			return;
		}
		// default folder icon
		boolean root = f.isRoot();
		Drawable d = (root ? imagesFolder[0] : imagesFolder[1]);
		v.setImageDrawable(d);
	}
	
	private static void initImages(Resources res, int[] drawables,
			BitmapDrawable[] images) {
		for (int i = 0; i < drawables.length; i++) {
			if (drawables[i] == -1) {
				images[i] = null;
				continue;
			}
			images[i] = new BitmapDrawable(BitmapFactory.decodeResource(res,
					drawables[i]));
		}
	}
	
	private static void initImages(Resources res, int[] drawables,
			Bitmap[] images) {
		for (int i = 0; i < drawables.length; i++) {
			if (drawables[i] == -1) {
				images[i] = null;
				continue;
			}
			images[i] = BitmapFactory.decodeResource(res, drawables[i]);
		}
	}
}
